//<SCRIPT LANGUAGE=javascript>
var includePath = "/manager/include";
var mangerLib = includePath + "/Manager_Lib.asp";
var dlPub = includePath + "/public/DownLoads.asp";
var updPub = includePath + "/public/Updates.asp";
var insert = false;
var refreshDecisionID = null;
var RSO;
var rs = new ActiveXObject("ADODB.Recordset");
var rsAddress = new ActiveXObject("ADODB.Recordset");
var updateRSPath = "/Application/include/UpdateRS.asp";
var getTableRoot = getScriptServer()+"/Application/include/getTable.asp?";
var tablePath = getTableRoot + "fetchTable('')";
var isCoAppl = 0;
var aEmplTable = new Array("t_Employment","v_CoEmployment");
var aApplInfo = new Array("v_ApplicantInfo","v_CoApplicantInfo");
var loadCount = 4;
var navy = 'navy',green='green',red='red';
var proc = {};
function selecto(sel){
	if( !sel.buffer )sel.buffer = '';
	if( event.keyCode == 13 )return;
	if( event.keyCode >= 37 && event.keyCode <= 40 )return sel.buffer = '';
	if( event.keyCode == 8 )sel.buffer = sel.buffer.substr(0,sel.buffer.length-1);
	else{
		var char = String.fromCharCode(event.keyCode).replace(/[^A-Z\d]/ig,"");
		sel.buffer += char;
	}
	event.keyCode = 40;
	event.cancelBubble = true;
	clearTimeout(proc[getFuncName()]);
	proc[getFuncName()] = setTimeout("findOption("+sel.uniqueID+")",1);
}
function findOption(sel){
	for( var i=0; i < sel.length; i++ )
		if( sel[i].text.match(new RegExp("^"+sel.buffer,"i")) )return sel[i].selected = true;
		sel.selectedIndex = -1;
}
function onLoadDOT(s){
	var DOT = onLoadDOT.index>=0?selDOT[onLoadDOT.index]:selDOT;
	delete onLoadDOT.index;
	s = s.replace(/<[^>]+>[\r\n]{1,2}/,"");
	for( ;DOT.length != 0; DOT.length = 0 ){};
	if( s.match(/^\d+$/) ){
		DOT[0] = new Option(s + " ocupations found!!!",0);
		DOT[0].style.color="red";
		return;
	}
	var aOptions = s.split("|");
	DOT[0] = new Option(aOptions.length/2 + " ocupations found.",0);
	DOT[0].style.color = "navy";
	for( var i=0; i < aOptions.length; i+=2 )
		DOT[DOT.length] = new Option(aOptions[i+1],aOptions[i]);
}
var navKeyCodes = {"37":true,"38":true,"39":true,"40":true,"9":true,"13":true};
function loadDOT(el,thread){
var thisName = getFuncName();
	if( event && navKeyCodes[event.keyCode+''] )return;
	clearTimeout(proc[thisName]);
	if( !thread || defined(onLoadDOT.index) )
		return proc[thisName] = setTimeout(getFuncName()+"("+el.uniqueID+",true)",1000);
	if( el.value.length < 4 )return;
	var index = getElementIndex(el);
	var DOT = index>=0?selDOT[index]:selDOT;
	onLoadDOT.index = index;
	for( ;DOT.length != 0; DOT.length = 0 ){};
	DOT[DOT.length] = new Option("*** Loading Ocupations ***",0);
	DOT[0].style.color = "green";
	oDownload.startDownload(dlPub+"?ocupations('"+el.value+"')", onLoadDOT);
}
function onDOTChange(el,thread){
	clearTimeout(proc[getFuncName()]);
	if( !thread )
		return proc[getFuncName()] = setTimeout(getFuncName()+"("+el.uniqueID+",true)",100);
	if( el.value != 0 )
		findPrevSiblingByAttr(el,"id","txtDOT").value = el[el.selectedIndex].text;
	var rs = getDataSrcRS(el);
	rs.AbsolutePosition = el.recordNumber;
	updateRSField(rs("DOT"),el.value);
}
function doMileageChange(){
	selCarDesc.locked = "1";
//	loadDSO(dcCarDesc,"/manager/include/public/Cars.asp?carByVIN('"+txtVIN.value+"',"+txtMileage.value+")");
	selCarDesc.locked = "";
}
function getCarDesc_Garbage(){
	if( !checkVIN(txtVIN.value) ){
		alert("Incorrect VIN number!");
		txtVIN.select();
		txtVIN.focus();
		return;
	}
	loadDSO(dcCarDesc,"/manager/include/public/Cars.asp?carByVIN('"+txtVIN.value+"',"+(txtMileage.value||1)+")");
}
function switchCoAppl(coAppl){
	if( coAppl ){
		loadDSO(dcApplicantCo,getLocation()+"?dcApplicantCo()",1);
		loadDSO(dcEmplCo,null,1);
		if( divEmplCo.innerHTML == "" ){
			onShowPersonalCo(divPersonal.innerHTML);
//			onShowEmplCo(divEmpl.innerHTML);
//			oDownload.startDownload(getTableRoot+"personal()", onShowPersonalCo);
			oDownload.startDownload(getLocation()+"?employment(1)", onShowEmplCo);
		}
	}
	divPersonalCo.style.display = divEmplCo.style.display = coAppl?"":"none";
}
function deleteCoAppl(){
	sendRequestXMLEx(getTableRoot,"remCoAppl");
	chkCoAppl.click();
}
function highlightCo(table){
	table.style.color = "darkred";
	table.style.backgroundColor = "cadetblue";
	table.style.marginLeft = table.style.marginBottom = "5px";
}
function onShowPersonalCo(s){
	divPersonalCo.innerHTML = s.replace(/#dcApplicant/ig,"#dcApplicantCo");
	var legend = divPersonalCo.getElementsByTagName("legend")[0];
	legend.innerHTML = "Co-Applicant personal info <input type='button' onclick='deleteCoAppl()' value='Delete Co-Signer'>";
	divPersonalCo.getElementsByTagName("fieldset")[0].style.color = "darkred";
	highlightCo(divPersonalCo.getElementsByTagName("table")[0]);;
	divPersonalCo.style.display = "";
}
function onShowEmplCo(s){
	if( isErrorEx(s) )return;
	divEmplCo.innerHTML = s.replace(/#dcEmpl/ig,"#dcEmplCo");
	divEmplCo.getElementsByTagName("legend")[0].innerHTML = "Co-Applicant employment";
	divEmplCo.getElementsByTagName("fieldset")[0].style.color = "darkred";
	highlightCo(divEmplCo.getElementsByTagName("table")[0]);
	divEmplCo.style.display = "";
}
function onShowEmpl(s){
	divEmpl.innerHTML = s;
	divEmpl.style.display='';
	if( --loadCount == 0 )setTimeout("divAll.style.display=''",100);
}
function onShowAddress(s){
	divAddress.innerHTML = s;
	divAddress.style.display='';
	if( --loadCount == 0 )setTimeout("divAll.style.display=''",100);
}
function onShowCarInfo(s){
	divCarInfo.innerHTML = s;
	divCarInfo.style.display='';
	if( --loadCount == 0 )setTimeout("divAll.style.display=''",100);
}
function onShowPersonal(s){
	divPersonal.innerHTML = s+divPersonal.innerHTML;
	divPersonal.style.display='';
	if( --loadCount == 0 )setTimeout("divAll.style.display=''",100);
}
function load(){
	this.focus();
	loadCarInfo();
	initDSOs(urlInFunc,urlOutFunc);
	loadDSOs(null,1);
	var txts = getElementsByName(new Array("input","select"));
	for( var i=0; i < txts.length; i++ )
		if( !txts[i].onkeydown )txts[i].onkeydown = doEnterKey;
//	loadTempls();
		reloadMe();
	selDealers.focus();
}
function unload_(dontQuit){
	if( !dontQuit && self == top ) sendRequestXMLEx("/manager/include/manager_lib.asp","setCurrApplID",0);
var o,oo = getElementsByName("object");
	for( o in oo )
		if( getDirtyStatus(oo[o]) )	updateDSOEx(oo[o],true);
}
function doAddOns(){
	if( typeof(spnAddOnDesc) == 'undefined' || typeof(spnAddOnDesc) == 'unknown' )return setTimeout("doAddOns()",1000);
	for( var i=0; i < spnAddOnDesc.length; i++ )
		getAddOnColor(spnAddOnDesc[i],AddOnPrice[i]);
	///locals
	function getAddOnColor(elDesc,elPrice){
		addPrice = parseInt(elPrice.innerText);
		var color = "navy";
		if( addPrice > 0 )color = "darkgreen";
		if( addPrice < 0 )color = "darkred";
		elDesc.style.color = color;
	}
}
function onLoadCarInfo(s){
	divCarInfo.innerHTML = s;
	doAddOns();
}
function loadCarInfo(){
	oDownload.startDownload("/manager/include/public/Downloads.asp?carInfo()", onLoadCarInfo);
}
function loadTempls(){
	if( dcApplicant.ReadyState != 4 )return setTimeout("loadTempls()",100);
	oDownload.startDownload(getTableRoot+"carInfo("+getApplicantRS()("MakeID").value+")", onShowCarInfo);
	oDownload.startDownload(getTableRoot+"personal()", onShowPersonal);
	oDownload.startDownload(getTableRoot+"residence()", onShowAddress);
	oDownload.startDownload(getTableRoot+"employment()", onShowEmpl);
}
function fillModels(makeID){
	fillSELECTFromRS(selCarID,getTableRoot+"getModels('"+makeID+"')","CarID","Name");
}
function getApplicantRS(){ return dcApplicant.Recordset }
function getCarInfoRS(){ return dcCarInfo.Recordset }
function getAdddressRS(){ return dcAddress.Recordset }
function getEmplRS(){ return dcEmpl.Recordset }
var oProgIDs = {};
var doTimeProcID = null;
function doTimeLength(el,thread){
	if( !el )return;
	if( !thread ){
		clearTimeout(doTimeProcID);
		return doTimeProcID = setTimeout(getFuncName()+"(document.all['"+el.uniqueID+"'],true)",100);
	}
	var dso = eval(getDataSrc(el));
	var rs = getDataSrcRS(el);
	var isEmplCo = dso.id.match(/dcEmplCo/i)?true:false;
	if( getTotalHomeLength(rs) < 4.9 )addNewAddress();
	else remResidense(rs);
	//Locals
	function addNewAddress(){
		rs.MoveLast();
		if( getHomeLength(rs.Fields) == 0 )return;
		if(!isEmplCo){
			insert = true;
			rs.AddNew("ApplicantID",rs("ApplicantID").value);
			rs("Years").value = rs("Months").value = 0;
			rs.MoveNext();
///			updateDSO(dso);
		}else{
///			updateDSOEx(dso,true);
			setTimeout('loadDSO('+dso.uniqueID+',getTableRoot+"addNewEmpl(\''+rs("CoApplicantID").value+'\',\''+dso.tableName+'\')",1)',1000);
		}
	}
	function remResidense(){
	var l=0,id;
		if( isEmplCo )updateDSO(dso,true);
		for( rs.MoveFirst(); !rs.EOF; rs.MoveNext() )
			if( (l += getHomeLength(rs.Fields)) > 4.9 )break;
		for( rs.MoveNext(); !rs.EOF; rs.MoveNext() )
			if( isEmplCo ){
				id = rs("ID").value;
				rs.Delete();
				sendRequestXMLEx(getTableRoot,"remEmpl",id);
			}else{
				rs.Delete();
			}
		if( isEmplCo )rs.UpdateBatch();
///		else updateDSO(dso);
	}
	function getTotalHomeLength(rs){
	var l=0;
		for( rs.MoveFirst(); !rs.EOF; rs.MoveNext() )
			l += getHomeLength(rs.Fields);
		return l;
	}
	function getHomeLength(fields){	return fields('Years').value + fields('Months').value/12; }
}
//function setOnChangeAddress(){var sels =	tblAddress.all.tags("SELECT");var inps =	tblAddress.all.tags("INPUT");	setIt(sels);setIt(inps);}
//function setIt(els){var e = new Enumerator(els);for( ; !e.atEnd(); e.moveNext() )	e.item().onchange = updateAddress;}
// Zip Stuff
function doZip(txtZip,thread){
	if( !thread )return setTimeout("doZip(document.all['"+txtZip.uniqueID+"'],true)",10);
	txtZip.value = txtZip.value.replace(/\D/g,"");
	if( txtZip.value.length < 5 )return;
	var index = txtZip.recordNumber-1;
	makeINPUT(txtCity[index] || txtCity,"*** Loading Cities ***");
	(spnState[index] || spnState).value = "XX";
	setTimeout("getZIP(document.all['"+txtZip.uniqueID+"'])",10);
}
function getZIP(txtZip){
	var co = sendRequestXMLEx(getTableRoot.replace(/[?]/,""),"getCityState",txtZip.value);
	if( co == "" )return;
	var oLocation = eval(co);
	var index = txtZip.recordNumber-1;
	(spnState[index] || spnState).value = oLocation.state;
	var city = txtCity[index] || txtCity;
	var aCities = oLocation.cities;
	var rs = getAdddressRS();
	rs.AbsolutePosition = index+1;
	rs(city.dataFld).value = aCities[0];
	if( aCities.length < 2 )
		makeINPUT(city);
	else{
		city.outerHTML = '<select id="'+city.id+'" dataFld="'+city.dataFld+'" onchange="updateDSO(this)"></select>';
		gaCities = aCities;
		setTimeout("fillCities("+city.id+","+index+")",100);
	}
	updateDSO(txtZip);
}
function fillCities(sel,index){
	sel = sel[index] || sel;
	sel.length = 0;
	for( var i=0; i < gaCities.length; i++ )
		sel[sel.length] = new Option(gaCities[i],gaCities[i]);
}
function getTablePath(tableName){ return tablePath.replace(/''/,"'"+tableName+"'") }
function getNumOfRowsInSal2Desc(txa){
var a = txa.value.match(/\n/g);
var l = Math.min(4,a?a.length+1:1);
	if( txa.rows != l ) txa.rows = l;
//	setDirtyStatus(txa,1);
	return l;
}
function updateSal2(el,thread){
	if( !thread )return doTimeProcID = setTimeout(getFuncName()+"(document.all['"+el.uniqueID+"'],true)",100);
var txa = txaSal2Desc;
var txt = txtSalary2;
var index = getElementIndex(el);
var selY = selSal2Year,selM = selSal2Month;
	if( index >= 0 ){	var ii = getDataSrc(el).match(/Co$/)?1:0;
		txa = txa[ii];txt = txt[ii];selY = selY[ii];selM = selM[ii];
	}
var yesDesc = selY.value > 0 || selM.value >= 6;
	if( !yesDesc && txa.value != "" ) txa.value = "";
	if( !yesDesc && txt.value != "0" ) txt.value = 0;
	txa.parentElement.parentElement.style.display = yesDesc?"":"none";
	txt.style.display = yesDesc?"":"none";
}
function makeINPUT(el,value){
	el.outerHTML = 
		'<input type="text" name="'+el.name+'" value="'+(value || "")+'"'+
						(el.maxLength?' maxLength="'+el.maxLength+'"':'')+
						(el.size?' size="'+el.size+'"':'')+
						(el.id?' id="'+el.id+'"':'')+
						(el.dataFld?' dataFld="'+el.dataFld+'"':'')+
		' />';
}
//SetExpression Stuff
//spnAddOnDesc.style.setExpression("color","getAddOnColor(spnAddOnDesc)");
//spnHighMileage.style.setExpression("color","getMileageColor(spnHighMileage)");
// Ocupation
function doDOT(selDOT){
var dso = divOcupation.dso;
	if( !isObject(dso) )alert(getFuncName()+":dso is not an object");
var rs = getDataSrcRS(dso);
var x = dso.oncellchange;
	dso.oncellchange = null;
	rs("DOT") = selDOT.value;
	dso.oncellchange = x;
	rs.AbsolutePosition = divOcupation.rec;
	rs("Position") = selDOT[selDOT.selectedIndex].text;
	selDOT.parentElement.style.display = 'none';
}
function doOcupation(el){
	divOcupation.style.display=divOcupation.style.display?'':'none';
	if( divOcupation.style.display )return;
	divOcupation.style.top = event.clientY-event.offsetY+el.offsetHeight-5;
	divOcupation.style.left = event.clientX-el.offsetLeft-event.offsetX-5;
	divOcupation.dso = getDataSrcObj(el);
	divOcupation.rec = el.recordNumber;
}
var ocupPath = getPath(location.href)+"/Ocupation.asp?";
function getSOCSubClasses(classID){
var sql = ocupPath+getFuncName()+"("+classID+")";
	fillSELECTFromRS(selSOCSubClasses,sql,"SOC Code","SOC Title");
}
function getDOTs(socCode){
var sql = ocupPath+getFuncName()+"('"+socCode+"')";
	fillSELECTFromRS(selDOTs,sql,"DOT Code","DOT Title");
}
// Database stuff
function updateDSO(el,thread){updateDSOEx(el,thread)}
function urlOutFunc(){ return getScriptServer()+updateRSPath }
function urlInFunc(dso){return getTablePath(dso.tableName) }
